package sdk.pay;

import android.app.Activity;

import java.util.List;

import sdk.pay.listener.Pay;
import sdk.pay.listener.PayGetPayStatusListener;
import sdk.pay.listener.PayGetPayTypeListener;
import sdk.pay.listener.PayUtilCallBack;
import sdk.pay.model.PayTypeModel;
import sdk.pay.model.TokenParam;
import sdk.pay.utils.PayExceptionUtil;
import sdk.pay.utils.dynamic.PayDownloadUtil;
import sdk.pay.utils.dynamic.PayDynamicHelper;

import static sdk.pay.constant.PayExceptionType.INIT_PARAMS_ERROR;

public class PayUtil implements Pay {
    private Pay mPay;

    public PayUtil(Activity activity, PayUtilCallBack callback) {
        this(activity, callback, false);
    }

    public PayUtil(Activity activity, PayUtilCallBack callback, boolean payStatusCallBack) {
        this(activity, null, null, null, null, null, callback, payStatusCallBack);
    }

    public PayUtil(Activity activity, String appid, String key, String vector, String systemName,
                   String code, PayUtilCallBack callback, boolean payStatusCallBack) {
        mPay = PayDynamicHelper.getInstance().getPayUtilsInterface(activity, appid, key,
                vector, systemName, code, callback, payStatusCallBack);
        if (null == mPay) {
            PayDownloadUtil.getInstance().dateReset();
            PayExceptionUtil.getInstance().saveExceptionFile("can't get PayUtilsInterface Class dynamic!");
            throw new RuntimeException("can't get PayUtilsInterface Class dynamic!");
        }
    }

    @Override
    public void getToken(TokenParam tokenParam, PayGetPayTypeListener getPayTypeListener) {
        if (isValidInitParams())
            mPay.getToken(tokenParam, getPayTypeListener);
        else
            showToast(INIT_PARAMS_ERROR.getMessage());
    }

    /**
     * @param tokenParam TokenParam
     * @param payType    int
     */
    @Override
    public void pay(TokenParam tokenParam, int payType) {
        if (isValidInitParams())
            mPay.pay(tokenParam, payType);
        else
            showToast(INIT_PARAMS_ERROR.getMessage());
    }

    /**
     * @param tokenParam TokenParam
     * @param payType    int
     */
    @Override
    public void getToken(TokenParam tokenParam, int payType) {
        if (isValidInitParams())
            pay(tokenParam, payType);
        else
            showToast(INIT_PARAMS_ERROR.getMessage());
    }

    @Override
    public void setPayParam(String... payParam) {
        mPay.setPayParam(payParam);
    }

    @Override
    public void getPayTypes(PayGetPayTypeListener getPayTypeListener) {
        if (isValidInitParams())
            mPay.getPayTypes(getPayTypeListener);
        else
            showToast(INIT_PARAMS_ERROR.getMessage());
    }

    @Override
    public List<PayTypeModel> getPayTypeModels() {
        if (isValidInitParams()) {
            return mPay.getPayTypeModels();
        } else {
            showToast(INIT_PARAMS_ERROR.getMessage());
        }
        return null;
    }

    @Override
    public void getPayParam(int typeId) {
        if (isValidInitParams())
            mPay.getPayParam(typeId);
        else
            showToast(INIT_PARAMS_ERROR.getMessage());
    }

    @Override
    public void getPayStatus(PayGetPayStatusListener getPayStatusListener) {
        mPay.getPayStatus(getPayStatusListener);
    }

    @Override
    public void destroy() {
        mPay.destroy();
    }

    @Override
    public void showToast(String msg) {
        mPay.showToast(msg);
    }

    @Override
    public boolean isValidInitParams() {
        return mPay.isValidInitParams();
    }

    @Override
    public void setPaying(boolean paying) {
        mPay.setPaying(paying);
    }
}
